package com.yiyuan.system.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.json.JSONObject;

import com.yiyuan.common.base.BaseAction;
import com.yiyuan.system.model.Group;
import com.yiyuan.system.model.GroupUser;
import com.yiyuan.system.model.Organ;
import com.yiyuan.system.service.ISystemService;

public class GroupAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	/* ************* 分页参数封装******************************************** */
	private String startrecord;
	private String endrecord;
	private String perpage;

	/* ************* form参数******************************************* */
	private String groupId;
	private String groupName;
	private String groupDesc;
	private String selUsers;
	/* ************* 输出结果封装***************************************** */
	private Group group;
	private List<Group> list;
	private List<GroupUser> groupUserList;
	private List<Organ> deptList;

	/* ************* 各功能函数******************************************* */
	// 资源引用
	@Resource
	private ISystemService systemService;

	
	// 进入组管理主页
	public String index() {
		return "toMain";
	}

	// 保存组
	public void save() {
		Group group = null;
		if (selUsers != null && !selUsers.equals("")) {
			selUsers = selUsers.substring(1);
		}
		String groupUsers[] = selUsers.split(",");

		if (this.groupId != null && !this.groupId.equals("")) {// 修改
			group = this.systemService.loadGroup(Integer.valueOf(groupId));
			group.setGroupName(this.groupName);
			group.setGroupDesc(this.groupDesc);
			group.setGroupUsers(groupUsers);
			this.systemService.updateGroup(group);
		} else {// 新增
			group = new Group();
			group.setGroupName(this.groupName);
			group.setGroupDesc(this.groupDesc);
			group.setUserId(this.getSessionUserId());
			group.setGroupUsers(groupUsers);
			this.systemService.addGroup(group);
		}
		try {
			ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
			ServletActionContext.getResponse().setContentType(
					"text/xml; charset=utf-8");
			ServletActionContext.getResponse().getWriter().println("1");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 组分页显示
	@SuppressWarnings("unchecked")
	public void listPage() {
		int start = 1;
		int end = 100;

		// 三个内置分页参数
		if (this.getStartrecord() != null && this.getEndrecord() != null
				&& this.getPerpage() != null) {
			start = Integer.parseInt(this.getStartrecord());
			end = Integer.parseInt(this.getEndrecord());
		}
		// 获得页数及每页记录数
		int totalRecord = systemService.getGroupPageCount(this.groupName);
		list = systemService.findGroupList(start, end,this.groupName);

		// 封装输出结果
		Map map = new HashMap();
		map.put("list", list);
		map.put("totalRecord", totalRecord);
		JSONObject jso = new JSONObject(map);

		try {
			ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
			ServletActionContext.getResponse().setContentType(
					"text/xml; charset=utf-8");

			ServletActionContext.getResponse().getWriter()
					.print(jso.toString());
		} catch (Exception e) {

		}
	}

	// 删除分组
	public void del() {
		String ret = null;
		try {
			this.systemService.deleteGroup(Integer.valueOf(this.groupId));
			ret = "delete";
		} catch (Exception e) {
			ret = "error";
		}

		try {
			ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
			ServletActionContext.getResponse().setContentType(
					"text/xml; charset=utf-8");
			ServletActionContext.getResponse().getWriter().print(ret);
		} catch (Exception e) {

		}

	}

	// 查看组详细
	public String view() {
		if (groupId != null && !groupId.equals("")) {
			this.group = this.systemService.loadGroup(Integer.valueOf(groupId));
			//加载部门列表
			this.deptList=this.systemService.findOrgList();
			//加载组成员列表
			this.groupUserList=this.systemService.findDDList(Integer.valueOf(groupId));
		}	
		return "toView";

	}

	// 进入组增加页面
	public String add() {
		deptList=this.systemService.findOrgList();
		return "toAdd";
	}

	/** **************************************************************** */
	public String getStartrecord() {
		return startrecord;
	}

	public void setStartrecord(String startrecord) {
		this.startrecord = startrecord;
	}

	public String getEndrecord() {
		return endrecord;
	}

	public void setEndrecord(String endrecord) {
		this.endrecord = endrecord;
	}

	public String getPerpage() {
		return perpage;
	}

	public void setPerpage(String perpage) {
		this.perpage = perpage;
	}

	public String getGroupId() {
		return groupId;
	}

	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

	public String getGroupName() {
		return groupName;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	public String getGroupDesc() {
		return groupDesc;
	}

	public void setGroupDesc(String groupDesc) {
		this.groupDesc = groupDesc;
	}

	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

	public List<Group> getList() {
		return list;
	}

	public void setList(List<Group> list) {
		this.list = list;
	}

	public List<GroupUser> getGroupUserList() {
		return groupUserList;
	}

	public void setGroupUserList(List<GroupUser> groupUserList) {
		this.groupUserList = groupUserList;
	}

	public String getSelUsers() {
		return selUsers;
	}

	public void setSelUsers(String selUsers) {
		this.selUsers = selUsers;
	}

	public List<Organ> getDeptList() {
		return deptList;
	}

	public void setDeptList(List<Organ> deptList) {
		this.deptList = deptList;
	}

}
